﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace TD.Interface.Credits
{
    /// <summary>
    /// The library class manages the different characters used for the credits.
    /// </summary>
    internal class Library : GameComponent
    {
        /// <summary>
        /// Get or set the instance of the library.
        /// </summary>
        public static Library Instance
        {
            get;
            private set;
        }

        /// <summary>
        /// The dictionary that holds all the textures of the characters used in the credits.
        /// </summary>
        private Dictionary<string, CharacterTexture> library;

        /// <summary>
        /// Creates a new library that manages the character textures.
        /// </summary>
        /// <param name="game">The game for whom to manage the textures.</param>
        public Library(Game game) :
            base(game)
        {
            Instance = this;
        }

        /// <summary>
        /// Initialize all the different fields.
        /// </summary>
        public override void  Initialize()
        {
            // Create the dictionary. This makes it able to link the characters to there representative.
            library = new Dictionary<string, CharacterTexture>();

            // Add all the alfabet characters to the library.
            //this[' '] = new CharacterTexture(Game, ' '); // Special empty character
            this['A'] = new CharacterTexture(Game, 'A');
            this['B'] = new CharacterTexture(Game, 'B');
            this['C'] = new CharacterTexture(Game, 'C');
            this['D'] = new CharacterTexture(Game, 'D');
            this['E'] = new CharacterTexture(Game, 'E');
            this['F'] = new CharacterTexture(Game, 'F');
            this['G'] = new CharacterTexture(Game, 'G');
            this['H'] = new CharacterTexture(Game, 'H');
            this['I'] = new CharacterTexture(Game, 'I');
            this['J'] = new CharacterTexture(Game, 'J');
            this['K'] = new CharacterTexture(Game, 'K');
            this['L'] = new CharacterTexture(Game, 'L');
            this['M'] = new CharacterTexture(Game, 'M');
            this['N'] = new CharacterTexture(Game, 'N');
            this['O'] = new CharacterTexture(Game, 'O');
            this['P'] = new CharacterTexture(Game, 'P');
            this['Q'] = new CharacterTexture(Game, 'Q');
            this['R'] = new CharacterTexture(Game, 'R');
            this['S'] = new CharacterTexture(Game, 'S');
            this['T'] = new CharacterTexture(Game, 'T');
            this['U'] = new CharacterTexture(Game, 'U');
            this['V'] = new CharacterTexture(Game, 'V');
            this['W'] = new CharacterTexture(Game, 'W');
            this['X'] = new CharacterTexture(Game, 'X');
            this['Y'] = new CharacterTexture(Game, 'Y');
            this['Z'] = new CharacterTexture(Game, 'Z');
        }

        /// <summary>
        /// Get a character from the library.
        /// </summary>
        /// <param name="key">The character for whom to get the texture representative.</param>
        /// <returns>The texture that represents the character.</returns>
        public CharacterTexture this[char param]
        {
            get
            {
                // We need both a STRING and CHAR representative of the key.
                string key = param.ToString().ToUpper();

                // Ensure the library contains the key.
                if (!library.ContainsKey(key))
                {
                    // Create the character if it doesn't exist yet.
                    library[key] = this['A'];
                }

                // Return the character as stored in the library.
                return library[key];
            }
            private set
            {
                // We need the STRING representative of the parameter.
                string key = param.ToString();

                // Ensure the library does not yet contain the key.
                if (!library.ContainsKey(key))
                {
                    // Add the value to the library.
                    library.Add(key, value);
                }
            }
        }
    }
}
